import { User } from "../../../../modeal/user.js"
const user = new User();
Page({
  data: {
    sites: {}
  },

  onShow: function () {
    this.getSite()  // 获取收货地址
  },

  // 获取收货地址
  getSite () {
    user.getSite().then(res => {
      this.setData({ sites: res })
    })
  },

  goSite (e) {
    let aid = e.currentTarget.dataset.aid
    wx.navigateTo({ url: "/pages/user/site/addSite/addSite?aid=" + aid })
  },

  // 删除地址
  del (e) {
    let aid = e.currentTarget.dataset.aid
    wx.showModal({
      title: '确定吗？',
      content: '删除后无法恢复哦',
      success: res => {
        res.confirm ? this.delSite(aid) : ""
      }
    })
  },

  delSite (aid) {
    user.delSite(aid).then(res => {
      // 删除陈宫 再次请求
      this.getSite()
    })
  },


  // 选择地址
  chooseSite (e) {
    let site = e.currentTarget.dataset.item
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2];  //上一个页面
    prevPage.setData({
      selectedSite: site,
      selectedSiteId: site.id
    });
    wx.navigateBack({ delta: 1 })
  },
})